package com.yourcompany.unismslog;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UniSmsLogModule extends WXModule {

    public void getSmsList(JSCallback callback) {
        try {
            Context context = mWXSDKInstance.getContext();
            ContentResolver cr = context.getContentResolver();
            Uri uri = Uri.parse("content://sms/");
            Cursor cursor = cr.query(uri, null, null, null, "date DESC");

            List<Map<String, Object>> smsList = new ArrayList<>();

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Map<String, Object> sms = new HashMap<>();
                    sms.put("id", cursor.getString(cursor.getColumnIndex("_id")));
                    sms.put("address", cursor.getString(cursor.getColumnIndex("address")));
                    sms.put("body", cursor.getString(cursor.getColumnIndex("body")));
                    sms.put("date", cursor.getLong(cursor.getColumnIndex("date")));
                    sms.put("type", cursor.getInt(cursor.getColumnIndex("type"))); // 1收 2发
                    smsList.add(sms);
                }
                cursor.close();
            }
            callback.invokeAndKeepAlive(smsList);
        } catch (Exception e) {
            Log.e("UniSmsLogModule", "读取短信异常", e);
            callback.invokeAndKeepAlive("error: " + e.getMessage());
        }
    }
}
